package com.zxshhy.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author PF
 * @date 2021/3/24 21:20
 * @Version 1.0
 */
@Slf4j
@Configuration
public class RabbitMqConfig {
    /**
     * 公共交换器
     */
    public final static String ZXSHHY_DIRECT_EXCHANGE="zxshhy_direct_exchange";

    /**
     * 订单队列
     */
    public final static String ZXSHHY_ORDER_QUEUE="zxshhy_order_queue";
    /**
     * 订单路由键
     */
    public final static String ZXSHHY_ORDER_ROUTINGKEY="zxshhy_order_routingkey";
    /**
     * 库存队列
     */
    public final static String ZXSHHY_STORAGE_QUEUE="zxshhy_storage_queue";
    /**
     * 库存路由键
     */
    public final static String ZXSHHY_STORAGE_ROUTINGKEY="zxshhy_storage_routingkey";
    /**
     * 账户余额队列
     */
    public final static String ZXSHHY_BALANCE_QUEUE="zxshhy_balance_queue";
    /**
     * 账户余额路由键
     */
    public final static String ZXSHHY_BALANCE_ROUTINGKEY="zxshhy_balance_routingkey";


    @Bean
    public DirectExchange commonDirectExchange(){
        log.info("RabbitMQ 创建直连类型公共交换器");
        return new DirectExchange(ZXSHHY_DIRECT_EXCHANGE,true,false);
    }

    @Bean
    public Queue orderQueue(){
        log.info("RabbitMQ 创建订单队列");
        return new Queue(ZXSHHY_ORDER_QUEUE,true);
    }
    @Bean
    public Queue storageQueue(){
        log.info("RabbitMQ 创建库存队列");
        return new Queue(ZXSHHY_STORAGE_QUEUE,true);
    }
    @Bean
    public Queue balanceQueue(){
        log.info("RabbitMQ 创建账户余额队列");
        return new Queue(ZXSHHY_BALANCE_QUEUE,true);
    }

    @Bean
    public Binding bindingOrderQueue(){
        log.info("RabbitMQ 交换器绑定订单队列");
        return BindingBuilder.bind(orderQueue()).to(commonDirectExchange()).with(ZXSHHY_ORDER_ROUTINGKEY);
    }
    @Bean
    public Binding bindingStorageQueue(){
        log.info("RabbitMQ 交换器绑定库存队列");
        return BindingBuilder.bind(storageQueue()).to(commonDirectExchange()).with(ZXSHHY_STORAGE_ROUTINGKEY);
    }
    @Bean
    public Binding bindingBalanceQueue(){
        log.info("RabbitMQ 交换器绑定账户余额队列");
        return BindingBuilder.bind(balanceQueue()).to(commonDirectExchange()).with(ZXSHHY_BALANCE_ROUTINGKEY);
    }
}
